﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//鼠标控制视野xhk6.24
public class ViewController : MonoBehaviour
{
    public static ViewController Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<ViewController>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("MapManager");
                _Instance = obj.AddComponent<ViewController>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static ViewController _Instance;

    private float MaxSize = 10.0f;
    public float MinSize = 3.0f;
 
    public float horizontalMoveSpeed = 1f;
    public float verticalMoveSpeed = 1f;
    public float speed = 5f;
    public float sensitivetyMouseWheel = 2f;
    public Camera MainCamera;

    private Vector3 msPos;
    private Vector3 pos;
    private float heightBorder;//鼠标控制边框
    private float widthBorder;

    Vector3 playerPos;
    bool isPlayerMoved;

    private float heightEdge;
    private float widthEdge;
    void Start()
    {
        MaxSize = Mathf.Min(7,Mathf.Min(ConstOfMap.height* ConstOfMap.yGap/2, (ConstOfMap.width * ConstOfMap.xGap / 2)*Screen.height/Screen.width));
        MainCamera.orthographicSize = Mathf.Min(MainCamera.orthographicSize, MaxSize);
        isPlayerMoved = true;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        if(Tag.playerPause)
        {
            return;
        }
        if (Input.GetAxis("Mouse ScrollWheel") != 0)//缩放
        {
            //Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
            MainCamera.orthographicSize -= Input.GetAxis("Mouse ScrollWheel")* sensitivetyMouseWheel;
            MainCamera.orthographicSize = Mathf.Clamp(MainCamera.orthographicSize, MinSize, MaxSize);
        }
        if (isPlayerMoved)
        {
            /////////////////////////////////////键盘控制视野
            msPos = Camera.main.WorldToScreenPoint(playerPos);
            heightBorder = Screen.height/4;
            widthBorder = Screen.width/4;

            heightEdge = MainCamera.orthographicSize;
            widthEdge = heightEdge * Screen.width / Screen.height;
            float x = 0.0f;
            float y = 0.0f;
            if (msPos.y - (Screen.height - heightBorder) > 0)
                y = msPos.y - (Screen.height - heightBorder);
            if (msPos.x - widthBorder < 0)
                x = msPos.x - widthBorder;
            if (msPos.y - heightBorder < 0)
                y = msPos.y - heightBorder;
            if (msPos.x - (Screen.width - widthBorder) > 0)
                x = msPos.x - (Screen.width - widthBorder);

            //Debug.Log(x + " + " + y);
            //transform.Translate(x, y, 0);
            //Debug.Log(Camera.main.ScreenToWorldPoint(new Vector3(x, y, 0))- Camera.main.ScreenToWorldPoint(Vector3.zero));
            transform.Translate(Camera.main.ScreenToWorldPoint(new Vector3(x, y, 0))- Camera.main.ScreenToWorldPoint(Vector3.zero));

        }
        else
        {
            /////////////////////////////////////鼠标控制视野
            msPos = Input.mousePosition;
            heightBorder = Screen.height / 100;
            widthBorder = Screen.width / 100;

            heightEdge = MainCamera.orthographicSize;
            widthEdge = heightEdge * Screen.width / Screen.height;
            float x = 0.0f;
            float y = 0.0f;



            if (msPos.y > Screen.height - heightBorder)
                y += verticalMoveSpeed;
            if (msPos.x < widthBorder)
                x -= horizontalMoveSpeed;
            if (msPos.y < heightBorder)
                y -= verticalMoveSpeed;
            if (msPos.x > Screen.width - widthBorder)
                x += horizontalMoveSpeed;


            x *= speed * Time.deltaTime;
            y *= speed * Time.deltaTime;
            transform.Translate(x, y, 0);

        }
        ///////////////限制边框
        pos = transform.position;
        pos.x = Mathf.Clamp(pos.x, widthEdge * 0.9f - ConstOfMap.xGap / 2, ConstOfMap.width * ConstOfMap.xGap - widthEdge * 0.9f - ConstOfMap.xGap / 2);
        pos.y = Mathf.Clamp(pos.y, heightEdge * 0.9f - ConstOfMap.yGap / 2, ConstOfMap.height * ConstOfMap.yGap - heightEdge * 0.9f - ConstOfMap.yGap / 2);
        transform.position = pos;
        isPlayerMoved = false;
    }


    public void SetPlayerPos(Vector3 player)
    {
        playerPos = player;
        isPlayerMoved = true;
    }

    public float DistanceToScreen(float dis)
    {
        return dis*Screen.height / (MainCamera.orthographicSize * 2);
    }
}
